package cn.demo.app.a.controller;

import cn.demo.app.a.domain.Test;
import cn.demo.app.a.domain.User;
import cn.demo.app.a.service.UserService;
import cn.ifloat.brick.basic.common.toolkit.StringUtils;
import cn.ifloat.brick.logger.annotations.OperationLog;
import cn.ifloat.brick.logger.annotations.PerformanceLog;
import cn.ifloat.brick.logger.common.PerformanceUnit;
import cn.ifloat.brick.logger.core.LogKit;
import cn.ifloat.brick.redis.core.RedissonClientCore;
import cn.ifloat.brick.sprofile.common.init.config.SpringUtil;
import cn.ifloat.brick.sprofile.redis.init.RedisClientContext;
import cn.ifloat.brick.sprofile.web.common.BasicController;
import cn.ifloat.brick.sprofile.web.init.config.WebEnvironmentConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RFuture;
import org.redisson.api.RTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Controller
@RequestMapping("test")
public class TestController extends BasicController {

    @Autowired
    private UserService userService;

    @Autowired
    private WebEnvironmentConfig webEnvironmentConfig;

    @Autowired
    private RedisClientContext redisContext;

    private UserService copyUserService;

    @PostConstruct
    public void init() {
        copyUserService = SpringUtil.getBean(UserService.class);
        logger.info("init method success");
    }

    /**
     * 查看列表
     *
     * @param model model
     * @return 结果
     */
    @RequestMapping("list")
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String list(Model model, /*@RequestBody*/ User user) {
        logger.info("访问到了搜索列表接口");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //name不为空时，组装模糊查询条件
        wrapper.like(StringUtils.isNotBlank(user.getName()), "name", user.getName());
        wrapper.eq(!Objects.equals(user.getId(), null), "id", user.getId());
        List<User> list = userService.list(wrapper);
        LogKit.system().info("测试user数量:" + list.size());
        model.addAttribute("items", list);
        return "user/test";
    }


    /**
     * 查看用户详情
     *
     * @param model Model
     * @param id    用户ID
     * @return 结果
     */
    @RequestMapping(value = "info", method = GET)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String getInfo(Model model, Long id) {
        logger.info("访问到了查看详情接口: {}", id);
        User user = userService.getById(id);
        model.addAttribute("items", user);
        return "user/detail";
    }

    /**
     * 删除用户信息
     *
     * @param model model
     * @param id    用户ID
     * @return 结果
     */
    @RequestMapping(value = "del", method = GET)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String del(Model model, Long id) {
        logger.info("访问到了删除接口: {}", id);
        userService.removeById(id);
        return list(model,new User());
    }


    /**
     * 修改用户信息
     *
     * @param user 内容
     * @return 结果
     */
    @RequestMapping(value = "update", method = PUT)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String update(Model model, @RequestBody User user) {
        userService.updateById(user);
        return list(model,new User());
    }

    @RequestMapping(value = "edit", method = GET)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String edit(Model model, Long id) {
        User user = userService.getById(id);
        model.addAttribute("items", user);
        return "user/update";
    }

    /**
     * 新增用户信息
     *
     * @return 结果
     */
//    @RequestMapping(value = "add", method = POST)
//    @PerformanceLog(unit = PerformanceUnit.MILLS)
//    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
//    public String add(Model model, @RequestBody User user) {
//        user.setCreateTime(new Date());
//        return list(model);
//    }
    @RequestMapping(value = "add", method = GET)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String add() {
        return "user/add";
    }

    /**
     * 王东辉测试用
     *
     * @param test 用户信息
     * @return 结果
     */
    @RequestMapping(value = "test", method = POST)
    @PerformanceLog(unit = PerformanceUnit.MILLS)
    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
    public String test(Model model, /*@RequestBody*/ Test test) {
        List<String> name = test.getName();
        for (String s : name) {
            System.err.println("s = " + s);
        }
        Long id = test.getId();
        System.err.println("name = " + name);
        logger.info("id :{}", id);
        logger.info("name :{}", name);
        return "list(model)";
    }

    /**
     * 条件查询列表
     *
     * @param model model
     * @return 结果
     */
//    @RequestMapping("query")
//    @PerformanceLog(unit = PerformanceUnit.MILLS)
//    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
//    public String query(Model model, @RequestBody User user) {
//        logger.info("访问到了搜索列表接口");
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        //name不为空时，组装模糊查询条件
//        wrapper.like(StringUtils.isNotBlank(user.getName()), "name", user.getName());
//        wrapper.eq(!Objects.equals(user.getId(), null), "id", user.getId());
//        List<User> list = userService.list(wrapper);
//        LogKit.system().info("测试user数量:" + list.size());
//        model.addAttribute("items", list);
//        return "user/test";
//    }
    RTopic tpc = null;
    @RequestMapping("ws")
        public String  ws(){

            RedissonClientCore core = redisContext.client();

            tpc = core.clientWrapper().getTopic("d3mo");
            core.strOps().set("A","A");
            tpc.addListenerAsync(String.class, (channel, msg) -> {
                System.out.println(msg);
            });

            return "user/ws";
        }

    @RequestMapping("wss")
    public String sendRMsg() {
        RFuture<Long> result = tpc.publishAsync("aaaaaaaaa");
        System.out.println(result);
        return "user/ws";
    }



}
